package com.yidu.BusinessData.service.impl;

import com.yidu.BusinessData.entity.MarketData;
import com.yidu.BusinessData.entity.Security;
import com.yidu.BusinessData.mapper.MarketDataMapper;
import com.yidu.BusinessData.service.MarketDataService;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 任俊杰
 * @version 1.0
 * @date 2022/12/5
 */

@Service
public class MarketDataImpl implements MarketDataService {
    @Resource
    private MarketDataMapper marketDataMapper;

    /**
     * 查询和分页
     *
     * @param page       当前页
     * @param limit      每页显示数
     * @param securityId 证券id
     * @param TIME       时间
     * @return map jihe
     */
    public List<MarketData> selectMarket(int page, int limit, String securityId, String TIME) {
        Map map = new HashMap();
        map.put("page", (page - 1) * limit);
        map.put("limit", limit);
        map.put("securityId", securityId);
        map.put("TIME", TIME);
        return marketDataMapper.selectMarket(map);
    }


    /**
     * 生成条件查询的方法
     * @param marketData 行情实体类
     * @return
     */
//    public String buildStrWhere(MarketData marketData){
//        StringBuffer strWhere=new StringBuffer("");
//        if(marketData.getMarketId()!=null && marketData.getMarketId().equals(""))
//        {
//            strWhere.append(" and s.securityId like '%" +marketData.getSecurityId() + "%'");
//        }
//        if(marketData.getTIME()!=null && marketData.getTIME().equals("")){
//            strWhere.append(" and md.DATE like '%" +marketData.getTIME() + "%'");//日期
//        }
//
//
//
////        if(marketData.getSecurityId()!=null && !marketData.getSecurityId().equals(""))
////        {
////            strWhere.append(" and s.securityId like '%" +marketData.getSecurityId() + "%'");
////        }
////        if(marketData.getTIME()!=null && marketData.getTIME().equals(""))
////        {
////            strWhere.append(" and md.DATE like '%" +marketData.getTIME() + "%'");//日期
////        }
//        System.out.println("strWhere="+strWhere);
//        return strWhere.toString();
//    }


    /**
     * 分页查询行数，根据条件来模糊查询后的行数
     *
     * @param securityId 查询条件 证券id
     * @param TIME       查询条件 时间
     * @return
     */
    public int selectMarketCount(String securityId, String TIME) {
        Map map = new HashMap();
        map.put("securityId", securityId);
        map.put("TIME", TIME);
        int count = marketDataMapper.selectMarketCount(map);
        return count;
    }


    /**
     * 获取最大id的方法
     *
     * @return
     */
    public String selectMaxFundId() {
        return marketDataMapper.selectMaxFundId();
    }


    /**
     * 删除（单个删除和多个删除）
     *
     * @param marketId 行情id
     * @return list 集合
     */
    public int deleteMarket(String marketId) {
        //将id转为数据
        String[] split = marketId.split(",");
        ArrayList<Object> list = new ArrayList<Object>();
        for (String id : split) {
            //将id存入集合
            list.add(id);
        }
        int i = marketDataMapper.deleteMarket(list);
        return i;
    }


    /**
     * 行情数据新增
     *
     * @param marketData 行情数据实体类
     * @return
     */
    public int addMarket(MarketData marketData) {
        return marketDataMapper.addMarket(marketData);
    }

    /**
     * 行情数据修改
     *
     * @param marketData 修改的行情对象
     * @return
     */
    public int updateMarket(MarketData marketData) {
        return marketDataMapper.updateMarket(marketData);
    }


    /**
     * 下拉表格的查询证券信
     * @param page         当前页
     * @param limit        每页显示数
     * @param securityName 证券名称
     * @return list 集合
     */
    public Map selectSecurity(int page, int limit, String securityName) {
        Map map = new HashMap();
        map.put("page", (page-1)*limit);
        map.put("limit", limit);
        map.put("term", append(securityName));
        List<Security> securities = marketDataMapper.selectSecurity(map);

        //查行数
        int count=marketDataMapper.selectSecurityCount(map);
        Map layUiMap = new HashMap();
        layUiMap.put("code", 0);
        layUiMap.put("msg", "");
        layUiMap.put("count", count);
        layUiMap.put("data", securities);
        return layUiMap;
    }

    /**
     * 下拉框证券信息，拼接条件的方法
     * @param securityName 证券名称
     * @return
     */
    private String append(String securityName) {
        String sql = "";
        if (securityName != null && !securityName.equals("")) {
            sql += " and securityName=" + securityName;
        }
        return sql;
    }

    /**
     * 分页查询下拉表格的查询证券信息
     * @param securityName 证券名称
     * @return
     */
    public int selectSecurityCount(String securityName) {
        Map map = new HashMap();
        map.put("securityName", securityName);
        int count = marketDataMapper.selectSecurityCount(map);
        return count;
    }
}
